package com.example.aixiangshou.runninggame;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.example.zhouwei.library.CustomPopWindow;
import com.x_memteor.base_master.application.MyApplication;
import com.x_memteor.base_master.base.BaseActivity;
import com.x_memteor.base_master.bean.ItemBean;
import com.x_memteor.base_master.bean.ScorBean;
import com.x_memteor.base_master.utils.RecycleViewUtils;
import com.x_memteor.base_master.view.LuckPanLayout;
import com.x_memteor.base_master.view.RotatePan;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LuckPanActivity extends BaseActivity implements LuckPanLayout.AnimationEndListener, CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.shan)
    ImageView shan;
    @BindView(R.id.yun)
    ImageView yun;
    @BindView(R.id.rotatePan)
    RotatePan rotatePan;
    @BindView(R.id.go)
    ImageView go;
    @BindView(R.id.luckpan_layout)
    LuckPanLayout luckpanLayout;
    @BindView(R.id.imv_sound)
    ImageView imvSound;
    @BindView(R.id.imv_close)
    ImageView imvClose;

    private List<ItemBean> list;

    private String[] strs;
    private Integer[] icons;
    private int size;

    private int gemTimes;
    List<ScorBean> scorBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initRootView() {
        setContentView(R.layout.activity_luck_pan);
    }

    @Override
    public void initView() {
        scorBeanList = new ArrayList<>();
        if (MyApplication.getInstance().getGameBean() != null && MyApplication.getInstance().getGameBean().getList() != null) {
            list = MyApplication.getInstance().getGameBean().getList();
            size = MyApplication.getInstance().getGameBean().getList().size();
            strs = new String[size];
            icons = new Integer[size];
            rotatePan.setPanNum(size);
            gemTimes = MyApplication.getInstance().getGameBean().getTurnuration();
        }
    }

    @Override
    public void initData() {
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                ItemBean itemBean = list.get(i);
                strs[i] = list.get(i).getName();
                icons[i] = list.get(i).getIcon();
                scorBeanList.add(new ScorBean(itemBean.getName(), itemBean.getIcon(), 0));
            }
            rotatePan.setImages(icons);
            rotatePan.setStrs(strs);
        }
    }

    @Override
    public void initListener() {
        luckpanLayout.setAnimationEndListener(this);
    }

    @Override
    public void endAnimation(int position) {
//        ToastUtils.showCenterToast(LuckPanActivity.this, "Position = " + position + "," + strs[position]);
        if (gemTimes > 0) {
            int i = scorBeanList.get(position).getTime();
            int put = i + 1;
            scorBeanList.get(position).setTime(put);
        }
        gemTimes--;
        if (gemTimes == 0) {
            MyApplication.getInstance().getGameBean().setScorBeanList(scorBeanList);
            startActivity(new Intent(LuckPanActivity.this, ShowScoreActivity.class));
        }
    }

    @OnClick(R.id.go)
    public void onViewClicked() {
        if (gemTimes > 0) {
            luckpanLayout.rotate(-1, 100);
        }
    }

    @OnClick({R.id.imv_sound, R.id.imv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imv_sound:
                showSoundPow();
                break;
            case R.id.imv_close:
                showClosePow();
                break;
        }
    }

    CustomPopWindow popSoundWindow;
    CustomPopWindow popCloseWindow;

    private void showClosePow() {
        View contentView = LayoutInflater.from(LuckPanActivity.this).inflate(R.layout.pop_closer, null);
        handleClose(contentView);

        popCloseWindow = new CustomPopWindow.PopupWindowBuilder(LuckPanActivity.this)
                .setView(contentView)//显示的布局
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()//创建PopupWindow
                .showAtLocation(imvSound.getRootView(), Gravity.CENTER, 0, 0);
    }


    private void showSoundPow() {
        View contentView = LayoutInflater.from(LuckPanActivity.this).inflate(R.layout.pop_center, null);
        handleSound(contentView);

        popSoundWindow = new CustomPopWindow.PopupWindowBuilder(LuckPanActivity.this)
                .setView(contentView)//显示的布局
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()//创建PopupWindow
                .showAtLocation(imvSound.getRootView(), Gravity.CENTER, 0, 0);

    }

    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleSound(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popSoundWindow != null) {
                    popSoundWindow.dissmiss();
                }
                String showContent = "";
                switch (v.getId()) {
                    case R.id.imv_close:
                        popSoundWindow.dissmiss();
                        break;
                }
            }
        };
        contentView.findViewById(R.id.imv_close).setOnClickListener(listener);

        CheckBox music = contentView.findViewById(R.id.checkbox_music);
        music.setOnCheckedChangeListener(this);
        CheckBox sound = contentView.findViewById(R.id.checkbox_sound);
        sound.setOnCheckedChangeListener(this);
    }

    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleClose(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_keep:
                        if (popCloseWindow != null) {
                            popCloseWindow.dissmiss();
                        }
                        break;
                    case R.id.tv_abandon:
                        BaseActivity.clearActicity();
                        startActivity(new Intent(LuckPanActivity.this, WelcomeActivity.class));
                        break;
                }
            }
        };
        contentView.findViewById(R.id.tv_keep).setOnClickListener(listener);
        contentView.findViewById(R.id.tv_abandon).setOnClickListener(listener);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.checkbox_music:
                if (isChecked) {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_sky));
                    buttonView.setText("ON");
                } else {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_fense));
                    buttonView.setText("OFF");
                }
                break;
            case R.id.checkbox_sound:
                if (isChecked) {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_sky));
                    buttonView.setText("ON");
                } else {
                    buttonView.setBackgroundColor(getResources().getColor(R.color.main_fense));
                    buttonView.setText("OFF");
                }
                break;
        }
    }
}

